<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.titleName" placeholder="参数名" clearable></el-input>
      </el-form-item>
       <el-form-item label="题目类型" >
        <el-cascader
          clearable
          placeholder="题目类型"
          @change="editCascader"
          :props="{ value: 'id',label:'typeName'}"
          style="width: 100%"
          v-model="subjectTypeTest"
          :options="subjectTypeOptions"
          :show-all-levels="false"
        ></el-cascader>
       
      </el-form-item>
      <el-form-item label="题目难度">
        <el-cascader
          clearable
          placeholder="题目难度"
          @change="editDifficlty"
          :props="{ value: 'id',label:'difficultyName' }"
          style="width: 100%"
          v-model="subjectDifficltyTest"
          :options="subjectDifficltyOptions"
          :show-all-levels="false"
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('reply:subject:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('reply:subject:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button @click="getSubjectModel">生成题目模板</el-button>
        <el-button @click="dialogVisible=true">上传题目文件</el-button>
        <span v-if="status"><a :href="modelUrl" @click="status=false">点击下载模板</a></span>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="分词id">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="题目类型">
        <template slot-scope="obj">
          {{obj.row.subjectType.typeName}}
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="题目难度">
        <template slot-scope="obj">
          {{obj.row.subjectDifficlty.difficultyName}}
        </template>
      </el-table-column>
      <el-table-column
        prop="titleName"
        header-align="center"
        align="center"
        label="标题">
         <template slot-scope="obj">
         <span v-html="obj.row.titleName"></span>
        </template>
      </el-table-column>
       <el-table-column
        prop="keyWordName"
        header-align="center"
        align="center"
        label="关键词">
         <template slot-scope="obj">
         <span v-html="obj.row.keyWordName"></span>
        </template>
      </el-table-column>
      <el-table-column
        prop="timeLimit"
        header-align="center"
        align="center"
        label="时间限制">
      </el-table-column>
      <el-table-column
        prop="subiectDesc"
        header-align="center"
        align="center"
        label="题目描述">
         <template slot-scope="obj">
         <span v-html="obj.row.subiectDesc"></span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!--弹跳框-->
    <el-dialog title="上传文件" :visible.sync="dialogVisible" width="30%">
      <el-upload class="upload-demo" drag
        action="http://localhost:8868/api/reply/subject/upload"
        multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">请上传您xlsx格式的题目表</div>
      </el-upload>
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="saveSubject">确 定</el-button>
    </el-dialog>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './subject-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          titleName:'',
          typeId:null,
          difficltyId:null
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        path: '',
        status: false,
        dialogVisible:false,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
         subjectTypeOptions:[],
        subjectDifficltyOptions:[],
          subjectTypeOptionsOne:[],
        subjectDifficltyOptionsOne:[],
          subjectTypeTest:'',
        subjectDifficltyTest:'',
        shuzu:[]
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList();
      this.findData();
    },
     created() {
      
    },
    methods: {
      // 获取数据列表
      getDataList () {
       
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl(`/reply/each/findEs/${this.pageIndex}/${this.pageSize}`),
          method: 'post',
          data: this.$http.adornParams({
            'titleName':this.dataForm.titleName,
            'typeId':this.dataForm.typeId,
            'difficltyId':this.dataForm.difficltyId
            
          })
        }).then(({data}) => {
            this.dataList = data.list
            for(let i=0;i<this.dataList.length;i++){
              this.dataList[i].subjectType = JSON.parse(this.dataList[i].subjectType)
              this.dataList[i].subjectDifficlty = JSON.parse(this.dataList[i].subjectDifficlty)
          }
          this.totalPage = data.total;
          this.dataListLoading = false
        })
      },
      getSubjectModel () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl(`/reply/subject/getModel`),
          method: 'get',
          data: this.$http.adornParams()
        }).then(({data}) => {
          this.modelUrl = data.path
          this.status = true
        })
      },
      //添加题目到题目
      saveSubject(){
        this.status=false
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/reply/subject/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
       async findData(){//页面加载获取数据
        this.$http({
          url: this.$http.adornUrl('/hope/reply/subjecttype/alls'),
          method: 'get'
        }).then(({data}) => {
          this.subjectTypeOptions = this.editData(data)
        })
        this.$http({
          url: this.$http.adornUrl('/hope/reply/subjecttype/all'),
          method: 'get'
        }).then(({data}) => {
          this.subjectTypeOptionsOne = data
        })
        await this.$http({
          url: this.$http.adornUrl('/hope/reply/subjectdifficulty/alls'),
          method: 'get'
        }).then(({data}) => {
          this.subjectDifficltyOptions = this.editData(data)
        })
        await this.$http({
          url: this.$http.adornUrl('/hope/reply/subjectdifficulty/all'),
          method: 'get'
        }).then(({data}) => {
          this.subjectDifficltyOptionsOne = data
        })
      },
        editCascader(){
        var shu=this.subjectTypeTest[this.subjectTypeTest.length-1]
        this.dataForm.typeId=shu;
          for(var i=0;i<this.subjectTypeOptionsOne.length;i++){
            if(this.subjectTypeTest[this.subjectTypeTest.length-1]===this.subjectTypeOptionsOne[i].id){
              this.dataForm.subjectType=this.subjectTypeOptionsOne[i]
             
            }
          }
          
      },
      editDifficlty(){
        var shu2=this.subjectDifficltyTest[this.subjectDifficltyTest.length-1];
        this.dataForm.difficltyId=shu2;
       // alert(shu2)
        for(var i=0;i<this.subjectDifficltyOptionsOne.length;i++){
          if(this.subjectDifficltyTest[0]===this.subjectDifficltyOptionsOne[i].id){
            this.dataForm.subjectDifficlty=this.subjectDifficltyOptionsOne[i]
          }
        }
      },
       editData(val){//处理流数据
        for (let i=0;i<val.length;i++){
          if (val[i].children.length === 0){
            val[i].children = undefined
          }else{
            this.editData(val[i].children)
          }
        }
        return val
      },
    }
  }
</script>
